/**
 * 
 */
package com.itboye.allshengmedical.sci.packet.resp;

import android.util.Log;

import com.example.allshengmedical.R;
import com.itboye.allshengmedical.sci.ResourceHelper;

/**
 * @author 贝贝 346551990@qq.com
 * @date 2016-12-9
 */
public class SelfCheckRespPacket extends BaseRespPacket {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7418006245135996432L;
	BaseRespPacket packet = new BaseRespPacket();
	boolean isRun = true;

	public SelfCheckRespPacket(BaseRespPacket packet) {
		this.clone(packet);
		this.packet = packet;
		this.setExpectDataLength(40);// 0x28的十进制
	}

	private byte get(int index) {
		// for (int i = 0; i < packet.getContent().length; i++) {
		// Log.e("hu  " + i,
		// Integer.parseInt(packet.getContent()[index] + "", 16) + "");
		// }
		return packet.getContent()[index];
	}

	/**
	 * 光强芯片是否读取成功
	 * 
	 * @return
	 */
	public boolean isLightIntensityChipReadSuccess() {
		// isLegalContent() &&

		return (packet.isLegalContent() && get(1) == 0x01);
	}

	/**
	 * 检测样品传感器暗电流是否超出范围
	 * 
	 * @return
	 */
	public boolean isSampleSensorDarkCurrentOutOfRange() {
		return (packet.isLegalContent() && get(2) == 0x01);
	}

	/**
	 * 检测LED灯传感器暗电流是否超出范围
	 * 
	 * @return
	 */
	public boolean isLEDSensorDarkCurrentOutOfRange() {
		return (packet.isLegalContent() && get(3) == 0x01);
	}

	/**
	 * 检测第一通道灯的光强是否正常
	 * 
	 * @return
	 */
	public boolean isFirstPassagewayNormal() {
		return (packet.isLegalContent() && get(4) == 0x01);
	}

	/**
	 * 检测第二通道灯的光强是否正常
	 * 
	 * @return
	 */
	public boolean isSecondPassagewayNormal() {
		return (packet.isLegalContent() && get(5) == 0x01);
	}

	/**
	 * 检测第三通道灯的光强是否正常
	 * 
	 * @return
	 */
	public boolean isThirdPassagewayNormal() {
		return (get(6) == 0x01);
	}

	/**
	 * 检测第四通道灯的光强是否正常
	 * 
	 * @return
	 */
	public boolean isFourthPassagewayNormal() {
		return (get(7) == 0x01);
	}

	/**
	 * 检测第一个制冷片温度是否正常
	 * 
	 * @return
	 */
	public boolean isFirstRefrigerationSheetTemperatureNormal() {
		return (this.get(8) == 0x01);
	}

	/**
	 * 检测第二个制冷片温度是否正常
	 * 
	 * @return
	 */
	public boolean isSecondRefrigerationSheetTemperatureNormal() {
		return (packet.isLegalContent() && this.get(9) == 0x01);
	}

	/**
	 * 检测热盖温度是否正常
	 * 
	 * @return
	 */
	public boolean isHotCoverTemperatureNormal() {
		return (packet.isLegalContent() && this.get(10) == 0x01);
	}

	public String lightIntensityChipNum() {
		return get(15) * 256 * 256 + get(16) * 256 + get(17) + ";";

	}

	private String LEDSensorDarkCurrentNum() {
		return (get(18) * 256 * 256 + get(19) * 256 + get(20) + ";");

	}

	private String FirstPassagewayNum() {
		return (get(21) * 256 * 256 + get(22) * 256 + get(23) + ";");

	}

	private String SecondPassagewayNum() {
		return ((get(24) * 256 * 256 + get(25) * 256 + get(26) + ";"));

	}

	private String ThreePassagewayNum() {
		return ((get(27) * 256 * 256 + get(28) * 256 + get(29) + ";"));

	}

	private String FourPassagewayNum() {
		return ((get(30) * 256 * 256 + get(31) * 256 + get(32) + ";"));

	}

	/**
	 * 自检是否成功
	 * 
	 * @return
	 */
	public boolean isSuccess() {
		return isLightIntensityChipReadSuccess()
				&& isSampleSensorDarkCurrentOutOfRange()
				&& isLEDSensorDarkCurrentOutOfRange()
				&& isFirstPassagewayNormal() && isSecondPassagewayNormal()
				&& isThirdPassagewayNormal() && isFourthPassagewayNormal()
				&& isFirstRefrigerationSheetTemperatureNormal()
				&& isSecondRefrigerationSheetTemperatureNormal()
				&& isFirstPassagewayNormal() && isHotCoverTemperatureNormal();
	}

	public String ModeOneTemp() {
		return packet.isLegalContent() + "," + packet.getContent(1) + ","
				+ this.get(2) + "," + this.get(3) + "," + this.get(4) + ","
				+ this.get(5) + "," + this.get(6) + "," + this.get(7) + ","
				+ this.get(8) + "," + this.get(9) + "," + this.get(10);

	}

	public boolean isRun() {
		return isRun;

	}

	/**
	 * 检测热盖温度是否正常
	 * 
	 * @return
	 */
	public String getErrorDesc() {

		StringBuilder builder = new StringBuilder();
		// builder.append("all" + ModeOneTemp() + ",");
		if (!isLightIntensityChipReadSuccess()) {
			isRun = false;
			builder.append(ResourceHelper
					.getString(R.string.serial_light_intensity_chip_read_fail));
		}
		if (!isSampleSensorDarkCurrentOutOfRange()) {
			isRun = false;
			builder.append(ResourceHelper
					.getString(R.string.serial_sample_sensor_dark_current_out_of_range)
					+ ":" + lightIntensityChipNum());
		}
		if (!isLEDSensorDarkCurrentOutOfRange()) {
			isRun = false;
			builder.append(ResourceHelper
					.getString(R.string.serial_led_sensor_dark_current_out_of_range)
					+ ":" + LEDSensorDarkCurrentNum());
		}

		if (!isFirstPassagewayNormal()) {
			isRun = false;
			builder.append(ResourceHelper
					.getString(R.string.serial_first_passageway_too_lower)
					+ ":" + FirstPassagewayNum());
		}

		if (!isSecondPassagewayNormal()) {
			isRun = false;
			builder.append(ResourceHelper
					.getString(R.string.serial_second_passageway_too_lower)
					+ ":" + SecondPassagewayNum());
		}

		if (!isThirdPassagewayNormal()) {
			isRun = false;
			builder.append(ResourceHelper
					.getString(R.string.serial_third_passageway_too_lower)
					+ ":" + ThreePassagewayNum());
		}

		if (!isFourthPassagewayNormal()) {
			isRun = false;
			builder.append(ResourceHelper
					.getString(R.string.serial_fourth_passageway_too_lower)
					+ ":" + FourPassagewayNum());
		}

		if (!isFirstRefrigerationSheetTemperatureNormal()) {
			isRun = false;
			builder.append(ResourceHelper
					.getString(R.string.serial_first_refrigeration_sheet_temperature_out_of_range));
		}

		if (!this.isSecondRefrigerationSheetTemperatureNormal()) {
			isRun = false;
			builder.append(ResourceHelper
					.getString(R.string.serial_second_refrigeration_sheet_temperature_out_of_range));
		}
		if (!isHotCoverTemperatureNormal()) {
			isRun = false;
			builder.append(ResourceHelper
					.getString(R.string.serial_second_refrigeration_sheet_hotlid_out_of_range));
		}
		return builder.toString();
	}

}
